package com.ingrid.javi.activities.validation;

import com.ingrid.javi.model.EditTextValidator;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;

public class FormFilledListener implements TextWatcher {

	private EditTextValidator[] validators;
	private Button formButton;

	public FormFilledListener(EditTextValidator[] editTextValidators, Button formButton) {
		this.validators = editTextValidators;
		this.formButton = formButton;
		
		for (int i = 0; i < editTextValidators.length; i++) {
			this.validators[i].addTextChangedListener(this);
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterTextChanged(Editable s) {
		boolean formValid = true;
		for (int i = 0; i < validators.length; i++) {
			if(!this.validators[i].isValid()){
				formValid = false;
			}
		}
		
		this.formButton.setEnabled(formValid);
	}

}
